package com.taobao.analyse.map;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;

import com.taobao.analyse.constants.AuctionAuctionsPosConstants;
import com.taobao.analyse.constants.SplitConstants;

/**
 * 统计商品所属于的类目的结构
 * 
 * @author Livis
 * 
 */
public class AuctionCategoryMapper extends MapReduceBase implements
		Mapper<Writable, Text, LongWritable, Text> {

	@Override
	public void map(Writable key, Text value,
			OutputCollector<LongWritable, Text> output, Reporter reporter)
			throws IOException {
		String line = value.toString();
		if (StringUtils.isEmpty(line)) {
			return;
		}
		String[] aa = StringUtils.splitPreserveAllTokens(line, SplitConstants.ITEM_REGX);
		if (aa.length < 65) {
			return;
		}
		String aid = aa[AuctionAuctionsPosConstants.AUCTION_ID];
		String cat1 = aa[AuctionAuctionsPosConstants.CATEGORY_LEVEL1];
		String cat2 = aa[AuctionAuctionsPosConstants.CATEGORY_LEVEL2];
		String cat3 = aa[AuctionAuctionsPosConstants.CATEGORY_LEVEL3];
		String cat4 = aa[AuctionAuctionsPosConstants.CATEGORY_LEVEL4];
		StringBuilder sb = new StringBuilder();
		sb.append(cat1).append(SplitConstants.SPLIT_UNDER_BAR).append(cat2)
				.append(SplitConstants.SPLIT_UNDER_BAR).append(cat3)
				.append(SplitConstants.SPLIT_UNDER_BAR).append(cat4);

		// 输出数据 商品id->cat1_cat2_cat3_cat4
		output.collect(new LongWritable(Long.parseLong(aid)),new Text(sb.toString()));
	}
	

}
